package com.zxw.mallafter.api;

import com.zxw.mallafter.model.AdminExtend;
import com.zxw.mallafter.service.UserService;
import model.User;
import model.UserExtend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import util.PageInfo;

import java.util.List;
import java.util.Map;

/**
 * 对用户操作
 */
@RestController
@RequestMapping(value = "/api/v1/user", produces = "application/json;charset=utf-8")
public class Userapi {
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //查询
    @GetMapping
    public Map<String, Object> getall(@RequestParam(defaultValue = "1") Integer pageNo,
                                      @RequestParam(defaultValue = "2147483647") Integer pageSize,
                                      UserExtend userExtend) {
        if (pageNo == null || pageNo < 1) {
            pageNo = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = Integer.MAX_VALUE;
        }
        PageInfo pageInfo = new PageInfo(pageNo, pageSize);
        List<UserExtend> getall = userService.getall(pageInfo, userExtend);
        com.github.pagehelper.PageInfo<UserExtend>
                page = com.github.pagehelper.PageInfo.of(getall);
        Map<String, Object> pi = Map.of("total", page.getTotal(),
                "pageNo", page.getPageNum(), "pageSize", page.getPageSize());
        return Map.of("success", true, "data", getall,"pi",pi);
    }
    //删除
    @DeleteMapping
    public ResponseEntity<Map<String,Object>> delete(@RequestBody Integer[] ids){
        int delete = userService.delete(ids);
        return ResponseEntity.ok().body(Map.of("success",delete>0));

    }
    @PostMapping
    public ResponseEntity<Map<String,Object>> insert(@RequestBody UserExtend userExtend){
        int insert = userService.insert(userExtend);
        return ResponseEntity.ok().body(Map.of("success",insert>0));
    }
        @PutMapping
    public ResponseEntity<Map<String,Object>> update(@RequestBody UserExtend userExtend){
        int update = userService.update(userExtend);
        return ResponseEntity.ok().body(Map.of("success",update>0));
    }
    //启用禁用
    @GetMapping("/status")
    public ResponseEntity<Map<String,Object>> updateStatus(@RequestParam Integer id,@RequestParam  String status){
        int updateStatus = userService.updateStatus(id,status);
        return ResponseEntity.ok().body(Map.of("success",updateStatus>0));
    }

}
